package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IGoodsDAO;
import com.twentyOne.ClothingLand.vo.Goods;

public class GoodsDAO extends BaseDAO implements IGoodsDAO {

	@Override
	public int addGoods(Goods goods) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(goods);
		int ID = goods.getId();
		tx.commit();
		session.close();
		return ID;
		
	}

	@Override
	public void deleteGoods(Goods goods) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete Goods where goodsId = " + goods.getGoodsId();
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public void modifyGoods(Goods goods) {

		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Goods set goodsId=:goodsId, colorId=:colorId, sizeId=:sizeId, brandId=:brandId, classId=:classId," +
				" styleId=:styleId, name=:name, price=:price, description=:description, remark=:remark where id=:id";
		Query query = session.createQuery(hql);
		query.setInteger("goodsId", goods.getGoodsId());
		query.setString("colorId", goods.getColorId());
		query.setString("sizeId", goods.getSizeId());
		query.setInteger("brandId", goods.getBrandId());
		query.setString("classId", goods.getClassId());
		query.setString("styleId", goods.getStyleId());
		query.setString("name", goods.getName());
		query.setDouble("price", goods.getPrice());
		query.setString("description", goods.getDescription());
		query.setString("remark", goods.getRemark());
		query.setInteger("id", goods.getId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public List<Goods> searchAllGoods() {
		
		Session session = getSession();
		String hql = "from Goods";
		Query query = session.createQuery(hql);
		List<Goods> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public Goods searchGoodsByID(int goodsId) {
		
		Session session = getSession();
		String hql = "from Goods as g where g.goodsId=" + goodsId;
		Query query = session.createQuery(hql);
		List <Goods> list = query.list();
		session.close();
		if(list.size() != 0) {
			Goods goods = list.get(0);
			return goods;
		}else {
			return null;
		}
		
		
		
	}

	@Override
	public List<Goods> searchGoodsByName(String name) {
		
		Session session = getSession();
		String hql = "from Goods as g where g.name like '%"+name+"%'";
		Query query = session.createQuery(hql);
		List<Goods> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}
	
	@Override
	public List<Goods> searchGoodsByClassId(String classId) {
		
		Session session = getSession();
		String hql = "from Goods as g where g.classId like '%"+classId+"%'";
		Query query = session.createQuery(hql);
		List<Goods> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public List<Goods> searchGoodsByPrice(Double minPrice, Double maxPrice) {
		
		Session session = getSession();
		String hql = "from Goods as g where g.price between "+minPrice+" and "+ maxPrice;
		Query query = session.createQuery(hql);
		List<Goods> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public List<Goods> searchGoodsByBrandId(int brandId) {
		
		Session session = getSession();
		String hql = "from Goods as g where g.brandId=" + brandId;
		Query query = session.createQuery(hql);
		List<Goods> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}	

}
